<?php
/**
 * @abstract All the resources have to be one word
 * 
 */
class Devel_IndexController extends Clu_Controller_Action_Hvn
{
    public function crawlImageAction()
    {
    	$imgUrl = "http://eurovinama.com/templates/themza_j15_07/images/blue/logo.gif";
    	$imgUrl = "http://vnexpress.net/Files/Subject/3B/A0/DB/0B/1.jpg";
    	$crawler = new Utils_Crawler();
    	$image = $crawler->crawlImage($imgUrl);
    	//$image = $response->getBody();
    	//now save to something
    	$destination = APP_PATH."/public/ufiles/1/img/dest2.jpg";
    	try {
    		$crawler->saveImage($image, $destination);
    	}
    	catch(Exception $e)
    	{
    		die_a($e);
    	}	
    	die("<img src='".$destination."' />");
    }
    
    public function filterHtmlAction()
    {
    	$form = new Zend_Form();
    	$form->setAttrib('id', 'new_article');
        $form->setAction('/en/devel/index/filter-html');

        $filterHtmlBody = new Clu_Filter_HtmlBody();
    	$fck = new Clu_Form_Element_FckEditor('body', array(
        		'fckOptions' => array (
        						'Height' => 300,
        						'Width'  => 500,
        						'ToolbarSet' => 'Comment'
        					)
       			 )
        );
        $fck->setLabel('Article Body');
        $fck->addFilter($filterHtmlBody ); //TODO: if our people, maybe we don't need to filter
		$form->addElement($fck);
		
		$submit = new Zend_Form_Element_Submit('submit', array( 
                                                               'label' => __('submit') ));
        
        $form->addElement($submit);
		$this->view->form = $form;
		
        if ($this->_request->isPost() && $this->view->form->isValid($_POST)) 
        {
        		
        
        }		
		
		$this->renderToHolder('filterHtml.phtml'); 
    }
    
 
    
    public function jqueryAjaxAction()
    {
    	$ret = array ("abc", "def");
    	$this->_helper->json->sendJson($ret);
    }

    public function jqueryIframeAction()
    {
 		die("hello");    
    }
    
    public function cacheAction()
    {
    	$frontendOptions = array(
           'lifetime' => 20,
           'debug_header' => true, // for debugging
           'regexps' => array(
		       // cache the whole site
       			'^/$' => array('cache' => true)
    		)
    	);
    	/*
    	
    	$frontendOptions = array(
               'lifetime' => 30, // ( seconds )cache lifetime of 2 hours 7200/120 = 60120 phut
               'automatic_serialization' => true
		);	
		*/
    	
        $backendOptions = array(
            'cache_dir' => APP_PATH . '/cache/ZendCache/', // Directory where to put the cache files
        	'file_name_prefix' => 'zend_cache_query',
            'hashed_directory_level' => 2
        );

        // getting a Zend_Cache_Core object
        $cache = Zend_Cache::factory('Page',
                                     'File',
                                     $frontendOptions,
                                     $backendOptions);
	    

		// see if a cache already exists:
        if(!$result = $cache->load('myresult')) {
            //cache miss; connect to the database
            $db = $this->db;
            $result = $db->fetchAll('SELECT * FROM users');
            $cache->save($result, 'myresult');
			echo "Cache miss " . time();
        	
        } else {
        	
        	//$cache->remove('myresult');
        	$t = $cache->load('myresult');
            //cache hit! shout so that we know
            echo "Cache hit " . time() . "<br/> <hr/>";		
            print_r($t);
            $cache->remove('myresult');
            
        }

		/*
        // we pass a unique identifier to the start() method
        if(!$cache->start('mypage')) {
            // output as usual:
        
            echo 'Hello world! ';
            echo 'This is cached ('.time().') ';
        
            $cache->end(); // the output is saved and sent to the browser
        }
        */
        //echo 'This is never cached ('.time().').';

        die('<br/> end of file');
		//die_a($result);
	}  
    
	public function addMiniControllerAction()
	{
		$this->renderToHolder('someScript.phtml');

		$miniRequest = array('module' => 'devel',
						'controller' => 'block', 
						'action' => 'testMini',
						'regionId' => 5, 'boxWeight' => 2
		);
		$this->addMiniAction($miniRequest);
		/*
		$miniRequest2 = array('module' => 'devel', 'controller' => 'block',
						'action' => 'testMini',
						'regionId' => 5, 'boxWeight' => 3
		);
		$this->addMiniAction($miniRequest2);
		*/
	}
	
	public function pageMetadataAction()
	{
		$this->view->placeholder('title')->set("shitty head");
		$this->view->placeholder('keywords')->set("my website keywords");
		$this->view->placeholder('description')->set("my website description");
	}
	
	public function svgalleryAction()
	{
	
		$this->renderToHolder('svgallery.phtml');
	}
}